package com.superman.supermarket.client.composite.orderlist;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.utils.StringUtils;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.composite.orderlist.opt.OrderListOpButton;
import com.superman.supermarket.client.composite.preorder.sku.PreorderSkuModule;
import com.superman.supermarket.client.event.PreorderPageEvent;
import com.superman.supermarket.client.event.PreorderPageEvent.PreorderPageEventType;
import com.superman.supermarket.client.gin.ShopGinjector;

/**
 * 订单列表商品模块
 * 
 * @author superman
 * @version 2018年8月12日下午5:10:20
 */
public class OrderListSkuModule extends PreorderSkuModule {

	private String spuId, props;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月12日 下午5:10:49
	 */
	public OrderListSkuModule() {
		skupanel.addDomHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("spuid", spuId);
				map.put("props", props);
				XGinjector.INSTANCE.getEventbusController().fire(new PreorderPageEvent(map, PreorderPageEventType.CLICK_SKU));

			}
		}, ClickEvent.getType());
	}

	private void dispathOrderOprationButtons(Map<String, Object> map) {
		optpanel.getElement().removeClassName("bd-1px ddd");
		optpanel.getElement().getStyle().setProperty("width", "auto");
		// 可操作的按钮
		if (isInOrderDetailState()) {
			// 详情中,需要展示操作按钮
			if (map.get("OP") != null && XJsonUtil.getOnlyList((JSONArray) map.get("OP")).size() > 0) {
				optpanel.setVisible(true);
				List<Object> ops = XJsonUtil.getOnlyList((JSONArray) map.get("OP"));
				for (Object object : ops) {
					int op = (int) Double.parseDouble(String.valueOf(object));
					OrderListOpButton orderListOpButton = new OrderListOpButton(map, op);
					optpanel.add(orderListOpButton);
				}
			}
		}
	}

	private native String getState()/*-{
									return $wnd.history.state;
									}-*/;

	private boolean isInOrderDetailState() {
		String state = getState();
		if (StringUtils.isEmpty(state))
			return false;
		else {
			String ss = state.split(":")[0];
			return ShopGinjector.INSTANCE.getMyHistoryMapper().getToken(ShopGinjector.INSTANCE.getMyPlaceFactory().getOrderDetailPlace())
					.indexOf(ss) > -1;
		}
	}

	@Override
	public void setData(Map<String, Object> map) {
		this.spuId = String.valueOf(map.get("SPU_ID"));
		this.props = String.valueOf(map.get("SKU_PROPS"));

		this.skuId = String.valueOf(map.get("SKU_ID"));
		this.stype = (int) Double.parseDouble(String.valueOf(map.get("STYPE")));
		this.num = (int) Double.parseDouble(String.valueOf(map.get("SKU_NUM")));

		skuimg.setUrl(String.valueOf(map.get("SKU_PIC")));
		skunamehtml.setText(String.valueOf(map.get("SKU_NAME")));
		skupropnameshtml.setText(String.valueOf(map.get("SKU_PROPS_NAME")));

		double buyPrice = Double.parseDouble(String.valueOf(map.get("SKU_BUY_PRICE")));

		salepricehtml.setHTML("¥&nbsp;" + buyPrice);
		numhtml.setHTML("x&nbsp;" + num);

		dispathOrderOprationButtons(map);

	}

}
